package com.ly.algorithm.DataStructures.sort;

import java.util.Arrays;

/**
 * @Classname QuickSort
 * @Description TODO
 * @Date 2020/4/24 15:26
 * @Author 冷心影翼
 */
public class QuickSort {

    public static void main(String[] args) {
//        int[] arr = { -9,0,78,23,-567,30};
        int[] arr = { -9,78,0,23,23,-567,70};
        quickSort(arr,0,arr.length-1);
        System.out.println(Arrays.toString(arr));
    }


    public static void quickSort(int[] arr,int left,int right) {
        int l = left;
        int r = right;
        int pivot = arr[(left+right)/2];
        while (l<r) {
            while (arr[l]<pivot) {
                l += 1;
            }
            while (arr[r] > pivot) {
                r -= 1;
            }
            if(l >= r) {
                break;
            }
            int temp = arr[l];
            arr[l] = arr[r];
            arr[r] = temp;

            if(arr[l] == pivot) {
                r -= 1;
            }
            if (arr[r] == pivot) {
                l += 1;
            }
        }

        if(l == r) {
            l += 1;
            r -= 1;
        }
        if(left < r) {
            quickSort(arr,left, r);
        }
        if(right > l) {
            quickSort(arr,l,right);
        }
    }
}
